import sys

from pyppeteer import launch

from constants import WINDWOS_CHROME_PATH, OS_CHROME_PATH, LINUX_CHROME_PATH


class BasePyppeteer:
    def __init__(self, url):
        self.url = url
        self.browser = None

    def get_chrome_path(self):
        return {
            'win32': WINDWOS_CHROME_PATH,
            'darwin': OS_CHROME_PATH,
            'linux': LINUX_CHROME_PATH
        }.get(sys.platform, None)

    async def create_browser(self, chrome_path):
        if not chrome_path:
            raise ValueError('Unsupported platform')
        if self.browser is None:
            self.browser = await launch({
                'executablePath': chrome_path,
                "headless": True,
            })

    async def close_browser(self):
        if self.browser:
            await  self.browser.close()

    async def setup_page(self):
        if not self.browser:
            raise ValueError('Browser not initialized')

        page = await self.browser.newPage()
        await page.setViewport({"width": 1980, "height": 1080});
        return page